﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Practices.Unity;
using SpectrumAnalyser.Common.Hosting;
using SpectrumAnalyser.Config.Client;
using SpectrumAnalyser.Config.Contract;
using SpectrumAnalyser.ModelObjects;
using SpectrumAnalyser.ModelObjects.Enum;

namespace SpectrumAnalyser.Hosts.Common
{
    /// <summary>
    /// Open hosts for specified services
    /// </summary>
    public class ServiceHostBoostrapperBase
    {
        private readonly IUnityContainer _unityContainer;

        public ServiceHostBoostrapperBase(IUnityContainer unityContainer)
        {
            this._unityContainer = unityContainer;
        }

        public virtual void Initialize()
        {
            IList<Instance> instances = GetInstances();

            foreach (var instance in instances)
            {
                var instanceContext = new SpectrumAnalyser.Common.InstanceContext.InstanceContext()
                    {
                        InstanceName = instance.Name,
                        ConnectionString = instance.ConnectionString,
                        InstanceType = instance.InstanceType
                    };

                this._unityContainer.RegisterInstance(typeof(SpectrumAnalyser.Common.InstanceContext.InstanceContext), instance.Name, instanceContext);
            }
        }

        private IList<Instance> GetInstances()
        {
            return this._unityContainer.Resolve<IConfigService>().GetInstances();
        }
    }
}
